﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Framework.Mvc.ViewModels;
using Shell.DomainDot;
using Framework.Domain;
using Framework.Poco;

namespace Framework.Mvc.Controllers
{
    /// <summary>
    /// 分类控制器
    /// </summary>
    public class CategoryController : Controller
    {
        private IDomainDotFactory factory;
        public CategoryController()
        {
            this.factory = DomainDotFactory.Factory;
        }

        /// <summary>
        /// 分类创建
        /// </summary>
        [HttpGet]
        public ActionResult Create(User currentUser)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var model = new CategoryViewModel();
                return View(model);
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 分类创建
        /// </summary>
        [HttpPost]
        public ActionResult Create(User currentUser, CategoryViewModel model)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                try
                {
                    // TODO: Add insert logic here
                    if (!Valid(this.ModelState, model.Instance))
                    {
                        return View(model);
                    }
                    var domain = factory.Create<CreateCategory>();
                    domain.Create(model.Instance);
                    return RedirectToAction("ArticleCategory", "Admin");
                }
                catch
                {
                    return View();
                }
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 分类修改
        /// </summary>
        [HttpGet]
        public ActionResult Edit(User currentUser, Guid identity)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var domain = factory.Create<SelectCategory>();
                var category = domain.Select(identity);
                if (category == null)
                {
                    return RedirectToAction("ArticleCategory", "Admin");
                }
                var model = new CategoryViewModel();
                model.Instance = category;
                return View(model);
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 分类修改
        /// </summary>
        [HttpPost]
        public ActionResult Edit(User currentUser, CategoryViewModel model)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                try
                {
                    // TODO: Add update logic here
                    if (!Valid(this.ModelState, model.Instance))
                    {
                        return View(model);
                    }
                    var domain = factory.Create<UpdateCategory>();
                    domain.Update(model.Instance);
                    return RedirectToAction("ArticleCategory", "Admin");
                }
                catch
                {
                    return View();
                }
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 分类删除
        /// </summary>
        [HttpGet]
        public ActionResult Delete(User currentUser, Guid identity)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                var domain = factory.Create<SelectCategory>();
                var category = domain.Select(identity);
                if (category == null)
                {
                    return RedirectToAction("ArticleCategory", "Admin");
                }
                var model = new CategoryViewModel();
                model.Instance = category;
                return View(model);

            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 分类删除
        /// </summary>
        [HttpPost]
        public ActionResult Delete(User currentUser, CategoryViewModel model)
        {
            if (currentUser != null && currentUser.IsAdmin)
            {
                try
                {
                    // TODO: Add delete logic here
                    var domain = factory.Create<DeleteCategory>();
                    domain.Delete(model.Instance.Id);
                    return RedirectToAction("ArticleCategory", "Admin");
                }
                catch
                {
                    return View();
                }
            }
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// 模型验证
        /// </summary>
        private bool Valid(ModelStateDictionary state, Category instance)
        {
            bool isValid = true;
            if (instance == null)
            {
                state.AddModelError("Instance", "实例为null。");
                isValid = false;
            }
            if (string.IsNullOrWhiteSpace(instance.Name))
            {
                state.AddModelError("Name", "标题为空。");
                isValid = false;
            }
            return isValid;
        }
    }
}
